package com.springboot.mybatis.service.threaddemo;

/**
 * @author sunyt31481
 * @date 2020/8/7 9:48
 * @description run方法内没有sleep或wait方法时，停止线程
 */
public class RightWayStopThreadWithoutSleep implements Runnable{
    @Override
    public void run() {
        int num = 0;
        while (!Thread.currentThread().isInterrupted() && num <= 100000 / 2){
            if (num % 10000 == 0){
                System.out.println(num+"是一万的倍数");
            }
            num++;
        }
        System.out.println("结束");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
        thread.start();
        try {
            //一秒后，即给个中断指令
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}

